//
//  CountryEditView.m
//  LPlates.app
//
//  Created by Andreas Schwelling on 23.11.07.
//	http://www.andi.de/licenseplates/
//
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.



#import "CountryEditView.h"

@implementation CountryEditView

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	td = [MobileVehicleRegPlates sharedInstance];
	//[setinvisible release];
	setinvisible =  [[NSMutableArray alloc] init];
	[setinvisible addObjectsFromArray:[td getPListParam:@"hiddenCountries"]];
	NSLog(@"setinv: %@",setinvisible);
	[setinvisible retain];
	
	
	
	
	//Get full screen app rect
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	
	
	
	[self buildTable];
	
	[table enableRowDeletion:YES animated:YES];
	return self;
}


-(void)fillTable
{
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	[countriesarr release];
	countriesarr =  [[NSMutableArray alloc] init];
	
	[visiblearr release];
	visiblearr =  [[NSMutableArray alloc] init];
	
	[invisiblearr release];
	invisiblearr =  [[NSMutableArray alloc] init];
	
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: @"/Applications/LPlates.app/default.plist"];
	
	NSArray *keys,*keys1;
	int k,j, count,count1;
	id key, value,  value1;
	
	keys = [dict allKeys];
	count = [keys count];
	for (j = 0; j < count; j++)
	  {
		  key = [keys objectAtIndex: j];
		  value = [dict objectForKey: key];
		  
		  keys1 = [value allKeys];
		  count1 = [keys1 count];
		  
		  
		  for (k=0;k< count1;k++) {
			  id key1 = [keys1 objectAtIndex: k];
			  
			  NSLog (@"Country: %@", key1);
			  [countriesarr addObject:key1];
			  
		  }
	  }
	
	[countriesarr sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	//NSLog(@"%@",countiesarr);
	int i = 0;
	for (i = 0;i < [countriesarr count];i++) {
		///
		//NSString *strr = [@"aaa" uppercaseString];
		//NSLog(@"%@",strr);//location,[countiesarr objectAtIndex:i]);
		if ([setinvisible containsObject:[countriesarr objectAtIndex:i]]) [invisiblearr addObject:[countriesarr objectAtIndex:i] ];
		else	 [visiblearr addObject:[countriesarr objectAtIndex:i] ];
		
		
	}
	
	if ([invisiblearr count]==0) [invisiblearr addObject:@""];
	if ([visiblearr count]==0) [visiblearr addObject:@""];
	
	NSLog(@" visiblesize: %d",[visiblearr count]);
	NSLog(@" insiblesize: %d",[invisiblearr count]);
	int invisiblestart = [visiblearr count];
	[visiblearr retain];
	
	[tabledata release];
	tabledata =  [[NSMutableArray alloc] init];
	[tabledata addObjectsFromArray:visiblearr];
	[tabledata addObjectsFromArray:invisiblearr];
	
	[_tableHeaders release];
	_tableHeaders = [[NSMutableArray alloc] init];
	
	// Each "header cell" here is a dictionary with two keys:
	// title points to a NSSTring of what we want the header to say
	// beginRow is a NSNumber integer that dictates which row this header will come before
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	[cellDict setObject:@"Visible" forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	NSLog(@"%@",cellDict);
	
	[_tableHeaders addObject: cellDict];
	cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	[cellDict setObject:@"Hidden" forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:invisiblestart] forKey:@"beginRow"];
	NSLog(@"%@",cellDict);
	
	[_tableHeaders addObject: cellDict];	
	
	
}


// Build the Table
- (void)buildTable
{
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(0.0f, 85.0f, 320.0f, 415.0f-45.0f) showSectionIndex:NO];
	[sectionList setDataSource:self];
	[sectionList reloadData];
	[self addSubview:sectionList];
	
	table = [sectionList table];
	[table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[table addTableColumn:packageColumn];
	[table setSeparatorStyle:1];
	[table setRowHeight:48.0f];
	[table setDelegate:self];
	[table setAllowsReordering:YES];
	
	
	
	//[self fillTable];
	//	cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	//[cellDict setObject:@"B" forKey:@"title"];
	//[cellDict setObject:[[NSNumber alloc] initWithInt:10] forKey:@"beginRow"];
	
	//[_tableHeaders addObject: cellDict];
	
	//Refresh the fileview table
	
	[self setAlpha:0.0f];
	[self addSubview:table];
	
	[self fillTable];
	
	
	
	
	
	
	
	[sectionList reloadData];
	
	
}

- (NSMutableArray*) getHiddenCountries
{
	
	return setinvisible;
}

- (BOOL)table:(UITable*)table canDeleteRow:( int)row {
	//	NSLog(@"canDeleteRow: %d",row);
	if ([[tabledata objectAtIndex:row] isEqual:@""]) return NO;
	if (row < [visiblearr count])  return YES; else return NO;
}

// this method must be declared so that rows can be moved
// if you don't want to delete rows, then return NO from table:canDeleteRow:
- (void)table:(UITable*)table deleteRow:(int)row {
	// the remove code
	//[activesArray removeObject:[NSString stringWithFormat:@"%d",row]];
	NSLog(@" detelet: %@",[tabledata objectAtIndex:row]);
	[setinvisible addObject:[tabledata objectAtIndex:row]];
	[self fillTable];
	
	[sectionList reloadData];
}

- (BOOL) table:(UITable *) table canInsertAtRow:(int) row 
{
	if ([[tabledata objectAtIndex:row] isEqual:@""]) return NO;
	// if (row == 2 && inserted==NO) return YES; else return NO;
	if (row < [visiblearr count])  return NO; else return YES;
	//return NO;
}

// this is requires to move rows, otherwise only the delete button object is shown
- (BOOL)table:(UITable*)table canMoveRow:(int)row{
	//	NSLog(@"canMoveRow %i", row);
	if ([[tabledata objectAtIndex:row] isEqual:@""]) return NO;
	return YES;
}

-(int)table:(UITable*)table movedRow:(int)row toRow:(int)dest{
	NSLog(@"table:movedRow:toRow: %i, %i", row, dest);
	
	if (dest<[visiblearr count]) {
		[setinvisible removeObject:[tabledata objectAtIndex:row]];
	} else {
		[setinvisible addObject:[tabledata objectAtIndex:row]];
	}
	[self fillTable];
	
	[sectionList reloadData];
	
	return dest;
}


- (void) table:(UITable *) table insertAtRow:(int) row 
{
    
	//[activesArray addObject:[NSString stringWithFormat:@"%d",row]];
	//[table reloadDataForInsertionOfRows:range animated:YES];
	[setinvisible removeObject:[tabledata objectAtIndex:row]];
	[self fillTable];
	
	[sectionList reloadData];
	// calling becomeFirstResponder here doesn't work - doesn't seem to while we'ere scrolling. Set a flag, then check when we
	// get a callback when we're done scrolling in scrollerDidEndAnimatedScrolling
	
    
}




- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	
	
	
	UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
	[cell setTitle: [tabledata objectAtIndex:row]];
	[cell createInsertControl];
	
	
	
	return cell;
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [tabledata count];
	
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [table selectedRow]];
	[td setCounty:[selectedCell title]];
	
	[td setDescription:[selectedCell title]];
	[td setBeforeDetailView:self];
	[td showDetails:1];
	//Do stuff here...
}

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
} 

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

@end